package org.view;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.jar.Attributes.Name;

import org.controller.ReservationController;
import org.controller.ReservationSystem;
import org.controller.ThirdPartPaymentSystem;
import org.domain.Customer;
import org.domain.Flight;
import org.objenesis.instantiator.basic.NewInstanceInstantiator;

public class Console {

	public static void main (String args[])
    {
		ReservationSystem system = new ReservationSystem();
		ThirdPartPaymentSystem paymentSystem = new ThirdPartPaymentSystem();
		ReservationController controller = new ReservationController(system, paymentSystem);
		Customer currentCustomer = null;
		Flight flightBeingBooked = null;
    
		System.out.println("----------------WELCOME TO PERRY AIR RESERVATION SYSTEM----------------");
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
	
		System.out.println("Today's Date: " + formatter.format(new Date()) + "\n");

    
		Scanner scanner = new Scanner(System.in);
		int userChoice = 0;
		displayMenu();
		userChoice = scanner.nextInt();
    
		while(userChoice > 0 && userChoice <= 5)
		{
			
			switch (userChoice) 
			{
            	case 1:
            			flightBeingBooked = getAvailableFlight(controller, scanner); 
            			 
            			break;
            	case 2:
            			if(flightBeingBooked == null){
            				System.out.println("Error. Please enter flight information \n"); 
            				break;
            			}          				
            			currentCustomer = enterCustomerInformation(controller, scanner);
            			if(currentCustomer != null)
            				System.out.println("Customer data entered successfully \n"); 
                     break;
            	case 3:  
            		if(flightBeingBooked == null){
        				System.out.println("Error. Please enter flight information \n"); 
        				break;
        			}     
            		if(currentCustomer == null){
        				System.out.println("Error. Please enter customer data \n"); 
        				break;
        			}  
        			String confirmationNumber = makePaymentAndFinalReservation(controller, scanner, currentCustomer, flightBeingBooked);
        			if(confirmationNumber != null){
        				System.out.println("Reservation is complete. \n Confirmation Number is " + confirmationNumber +"\n");
        			}
        			break;
            	case 4: 
                     controller.generateDailyReport();
                     break;
            	case 5:
            		System.out.println("Enter the date of the report you wish to view in mm/dd/yyyy format: ");
           		 	String dateOfReport = scanner.next();
                    controller.viewPastReport(dateOfReport);
                    break;
            	default: 
                     System.out.println("Not Valid Option");
                     break;

			}
			
			displayMenu();
			userChoice = scanner.nextInt();
		}
    }

	private static String makePaymentAndFinalReservation(
			ReservationController controller, Scanner scanner, Customer customer, Flight flight) {
		System.out.println("Does the customer have any bags (true/false)? : ");
		boolean hasBag = scanner.nextBoolean();
		System.out.println("Does the customer need disability assistance (true/false)?: ");
		boolean disabilityAssistanceNeeded = scanner.nextBoolean();
		System.out.println("Enter the 16-digit credit card number of the customer: ");
		String creditCardNumber = scanner.next();
		System.out.println("Enter the cvv number at the back of the credit card: ");
		String cvv = scanner.next();
		System.out.println("Enter the expiry date of the credit card: ");
		String expiryDate = scanner.next();
		System.out.println("Enter the billing address zipcode: ");
		String billingZipCode = scanner.next();
		if(!controller.makePayment(creditCardNumber, cvv, expiryDate, billingZipCode)){
			 System.out.println("Error. Enter the data correctly \n");
			 return null; 
		 }
		return controller.makeReservation(customer, flight, hasBag, disabilityAssistanceNeeded);
	}

	private static Customer enterCustomerInformation(
			ReservationController controller, Scanner scanner) {
		 System.out.println("Enter the first name of the customer with first letter Capital case (John): ");
		 String firstName = scanner.next();
		 System.out.println("Enter the last name of the customer with first letter Capital case (John): ");
		 String lastName = scanner.next();
		 System.out.println("Enter the date of birth of the customer in mm/dd/yyyy format: ");
		 String dateOfBirth = scanner.next();
		 System.out.println("Enter the phone number of the customer in xxx-xxx-xxxx: ");
		 String phoneNumber = scanner.next();
		 if(!controller.validateCustomerInfo(firstName, lastName, dateOfBirth, phoneNumber)){
			 System.out.println("Error. Enter the data correctly \n");
			 return null;
		 }
		 return controller.saveCustomerData(firstName, lastName, dateOfBirth, phoneNumber);
	}

	private static Flight getAvailableFlight(ReservationController controller,
			Scanner scanner) {
		 System.out.println("Enter the date of flight in mm/dd/yyyy format: ");
		 String dateOfFlight = scanner.next();
		 System.out.println("Enter the flight departure location (SFO/LAS): ");
		 String flightDeparture = scanner.next();
		 System.out.println("Enter the flight destination location (SFO/LAS): ");
		 String flightDestination = scanner.next();
		 if(controller.validateFlightInfo(dateOfFlight, flightDeparture, flightDestination)){
			 Flight availableFlight = controller.isFlightAvailable(dateOfFlight, flightDeparture, flightDestination);
			 
			 if(availableFlight != null)
			 { 
				 System.out.println("Yes. A seat is available on the flight \n"); 
				 return availableFlight;
			 }
			 else
				 System.out.println("No flight is available \n");
		 }
		 else
		 {
			 System.out.println("Error. Enter the data correctly \n");
		 }
		 return null;
	}

	private static void displayMenu() {

        System.out.println("Choose from the following commands: ");
        System.out.println("1: Check if a seat on the flight is available ");
        System.out.println("2: Enter customer details");
        System.out.println("3: Make reservation");
        System.out.println("4: Generate daily report");
        System.out.println("5: View past report");
        System.out.println("-----------------------------------------------");
	}
	
}

